﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Service;

namespace PasswordProvider.ViewModel
{
    sealed class CreditCardListItemViewModel : SiteListItemViewModel
    {
        internal CreditCardListItemViewModel(CreditCardSite site)
            : base(site)
        {
           
        }

        #region Base class abstract overrides

        public override string GroupDisplayName
        {
            get { return Strings.ListItem_CreditCard_DisplayName; }
        }
        protected override INotifyChangesCommited GoToSiteDetails()
        {
            return (INotifyChangesCommited)NavigationManager.GoToSiteDetailsView(Site as CreditCardSite, false);
        }
        protected override string GenerateDisplayName()
        {
            return CreateDisplayName(Site as CreditCardSite);
        }
        protected override string GenerateToolTip()
        {
            return CreateToolTip(Site as CreditCardSite);
        }

        protected override bool NavigateToWebSiteCommandCanExecute()
        {
            CreditCardSite site = (CreditCardSite)Site;
            return (site.Website != null && site.Website.Url != null);
        }

        protected override void NavigateToWebSiteCommandExecuted()
        {
            base.NavigateToWebSite(((CreditCardSite)Site).Website);
        }

        #endregion

        #region Private Helper Methods

        private static string CreateDisplayName(CreditCardSite site)
        {
            return site.Name;
        }
        private static string CreateToolTip(CreditCardSite site)
        {
            StringBuilder toolTip = new StringBuilder();

            if (!String.IsNullOrEmpty(site.Description))
            {
                toolTip.Append(site.Description);
                toolTip.AppendLine();
            }
            if (site.Tags != null && site.Tags.Count > 0)
            {
                toolTip.Append("Tags: ");
                foreach (string tag in site.Tags)
                {
                    toolTip.Append(tag);
                    if (tag != site.Tags.Last())
                    {
                        toolTip.Append(", ");
                    }
                }
                toolTip.AppendLine();
            }
            if (site.CardNumber != null)
            {
                toolTip.AppendFormat("Last access date: {0}", site.CardNumber.LastAccessDate);
                toolTip.AppendLine();
                toolTip.AppendFormat("Access count: {0}", site.CardNumber.AccessCount);
            }

            return toolTip.ToString();

        }

        #endregion // Private Helper Methods
        
    }
}
